/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package handlers.itemhandlers;

import javolution.util.FastMap;
import net.sf.l2j.gameserver.datatables.SkillTable;
import net.sf.l2j.gameserver.handler.IItemHandler;
import net.sf.l2j.gameserver.model.L2ItemInstance;
import net.sf.l2j.gameserver.model.L2Skill;
import net.sf.l2j.gameserver.model.actor.L2Playable;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.actor.instance.L2PetInstance;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance.TimeStamp;
import net.sf.l2j.gameserver.model.entity.TvTEvent;
import net.sf.l2j.gameserver.network.SystemMessageId;
import net.sf.l2j.gameserver.network.serverpackets.ActionFailed;
import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;


public class ItemSkills implements IItemHandler
{
	private static final int[] ITEM_IDS =
	{
		6403,6406,6407,13268,13269,10549,10550,10551,10552,10553,
		10554,10555,10556,10557,10558,10559,10560,10561,10562,10563,
		10564,10565,10566,10567,10568,10569,10570,10571,10572,10573,
		10574,10575,10576,10577,10578,10579,10580,10581,10582,10583,
		10584,10585,10586,10587,10588,10589,10591,10592,10593,10594,
		10595,10608,10609,10610,12768,12769,12770,12771,13552,13553,
		13554,13728,14170,14171,14172,14173,14174,14175,14176,14177,
		14178,14179,14180,14181,14182,14183,14184,14185,14186,14187,
		14188,14189,14190,14191,14192,14193,14194,14195,14196,14197,
		14198,14199,14200,14201,14202,14203,14204,14205,14206,14207,
		14208,14209,14210,14211,14212,14213,14214,14215,14216,14217,
		14218,14219,14220,14221,14222,14223,14224,14225,14226,14227,
		20353,20364,20365,20366,20367,20368,20369,20370,20371,22022,
		22023,22024,22025,22026,22039,22040,22041,22042,22043,22044,
		22045,22046,22047,22048,22049,22050,22051,22052,22053,22089,
		22090,22091,22092,22093,22094,22095,22096,22097,22098,22099,
		22100,22101,22102,22103,22104,22105,22106,22107,22108,22109,
		22110,22111,22112,22113,22114,22115,22116,22117,22118,22119,
		22120,22121,22122,22123,22124,22125,22126,22127,22128,22129,
		22130,22131,22132,22133,22134,22135,22136,22137,22138,22139,
		22140,22141,22142,22143,22149,22150,22151,22152,22153,
	};
	
	/**
	 * 
	 * @see net.sf.l2j.gameserver.handler.IItemHandler#useItem(net.sf.l2j.gameserver.model.actor.L2Playable, net.sf.l2j.gameserver.model.L2ItemInstance)
	 */
	public void useItem(L2Playable playable, L2ItemInstance item)
	{
		L2PcInstance activeChar; // use activeChar only for L2PcInstance checks where cannot be used PetInstance
		
		if (playable instanceof L2PcInstance)
			activeChar = (L2PcInstance) playable;
		else if (playable instanceof L2PetInstance)
			activeChar = ((L2PetInstance) playable).getOwner();
		else
			return;
		if (activeChar.isInOlympiadMode())
		{
			activeChar.sendPacket(new SystemMessage(SystemMessageId.THIS_ITEM_IS_NOT_AVAILABLE_FOR_THE_OLYMPIAD_EVENT));
			return;
		}
		if (!TvTEvent.onScrollUse(playable.getObjectId()))
		{
			playable.sendPacket(ActionFailed.STATIC_PACKET);
			return;
		}
		int skillId;
		int skillLvl;

		final String[] skills = item.getEtcItem().getSkills();
		if (skills != null)
		{
			for (String skillInfo : skills)
			{
				String[] skill = skillInfo.split("-");
				if (skill != null && skill.length == 2)
				{
					skillId = Integer.parseInt(skill[0]);
					skillLvl = Integer.parseInt(skill[1]);
					if (skillId > 0 && skillLvl > 0)
					{
						L2Skill itemSkill = SkillTable.getInstance().getInfo(skillId, skillLvl);
						if (itemSkill != null)
						{
							if (!itemSkill.checkCondition(playable, playable, false))
					        	return;
							if ( playable.isSkillDisabled(skillId))
							{
								reuse(activeChar,itemSkill);
								return ;
							}
							// pets can use items only when they are tradeable
							if (playable instanceof L2PetInstance && !item.isTradeable())
								activeChar.sendPacket(new SystemMessage(SystemMessageId.ITEM_NOT_FOR_PETS));
							else
							{
								// send message to owner
								if (playable instanceof L2PetInstance)
								{
									SystemMessage sm = new SystemMessage(SystemMessageId.PET_USES_S1);
									sm.addString(itemSkill.getName());
									activeChar.sendPacket(sm);
								}
								else
								{
									switch (skillId)
									{
										// short buff icon for healing potions
										case 2031:
										case 2032:
										case 2037:
										case 26025:
										case 26026:
											int buffId = activeChar._shortBuffTaskSkillId;
											// greater healing potions
											if (skillId == 2037 || skillId == 26025)
												activeChar.shortBuffStatusUpdate(skillId, skillLvl, itemSkill.getBuffDuration()/1000);
											// healing potions
											else if ((skillId == 2032 || skillId == 26026) && buffId !=2037 && buffId != 26025)
												activeChar.shortBuffStatusUpdate(skillId, skillLvl, itemSkill.getBuffDuration()/1000);
											// lesser healing potions
											else
											{
												if (buffId != 2037 && buffId != 26025 && buffId != 2032 && buffId != 26026)
													activeChar.shortBuffStatusUpdate(skillId, skillLvl, itemSkill.getBuffDuration()/1000);
											}
											break;
									}
								}
								if (itemSkill.isPotion())
									playable.doSimultaneousCast(itemSkill);
								else
								{
									playable.stopMove(null);
									playable.doCast(itemSkill);
								}
								if (itemSkill.getReuseDelay() > 0)
									activeChar.addTimeStamp(skillId, itemSkill.getReuseDelay());
							}
						}
					}
				}
			}
		}
	}
	
	private void reuse(L2PcInstance player,L2Skill skill)
	{
		SystemMessage sm = null;
    	FastMap<Integer, TimeStamp> timeStamp = player.getReuseTimeStamp();
			
    	if (timeStamp != null && timeStamp.containsKey(skill.getId()))
    	{
    		int remainingTime = (int)(player.getReuseTimeStamp().get(skill.getId()).getRemaining()/1000);
    		int minutes = (remainingTime%3600)/60;
    		int seconds = (remainingTime%60);
    		if (minutes > 0)
    		{
    			sm = new SystemMessage(SystemMessageId.S2_MINUTES_S3_SECONDS_REMAINING_FOR_REUSE_S1);
    			sm.addSkillName(skill);
    			sm.addNumber(minutes);
    		}
    		else
    		{
    			sm = new SystemMessage(SystemMessageId.S2_SECONDS_REMAINING_FOR_REUSE_S1);
    			sm.addSkillName(skill);
    		}
    		sm.addNumber(seconds);
    	}
    	else
    	{
    		sm = new SystemMessage(SystemMessageId.S1_PREPARED_FOR_REUSE);
    		sm.addSkillName(skill);
    	}
    	player.sendPacket(sm);
	}
	/**
	 * 
	 * @see net.sf.l2j.gameserver.handler.IItemHandler#getItemIds()
	 */
	public int[] getItemIds()
	{
		return ITEM_IDS;
	}
}
